//
//  ShareActionSheet.m
//  ShareActionSheetTest
//
//  Created by on 15-9-15.
//  Copyright (c) 2015年  All rights reserved.
//

#import "ShareActionSheet.h"
#import "AppDelegate.h"
#import "VDJConstant.h"

#define kButtonCountPerRow 4
#define kIntervalWithButtonsX ((kScreenWidth - 4*60) / 5)
#define kIntervalWithButtonsY 10
#define kHeaderHeight 70
#define kBottomHeight 20
#define kCancelButtonHeight 46

@implementation ShareActionSheet
@synthesize buttons;
@synthesize backgroundImageView;
@synthesize delegate;

- (id)initWithButtons:(NSArray *)buttonArray
{
    self = [super init];
    self.buttons = buttonArray;
    if (self) {
        // Initialization code
        
        //背后透明View
        _coverView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _coverView.backgroundColor = [UIColor colorWithRed:51.0f/255 green:51.0f/255 blue:51.0f/255 alpha:0.6f];
        _coverView.hidden = YES;
        self.backgroundColor = kGetColor(234, 234, 234);
        
        
        //头部的Label
        _headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, kScreenWidth, 20)];
        _headerLabel.text = @"分享到";
        _headerLabel.textAlignment = NSTextAlignmentCenter;
        _headerLabel.font = [UIFont systemFontOfSize:20];
        _headerLabel.textColor = [UIColor grayColor];
        [self addSubview:_headerLabel];
        
        //分享的按钮
        for (int i = 0; i < [self.buttons count]; i++) {
           ShareActionSheetButton * button = [self.buttons objectAtIndex:i];
            button.imgButton.tag = i;
            [button.imgButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview: button];
        }
        
        //取消按钮
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _cancelButton.titleLabel.font=[UIFont systemFontOfSize:22];
        _cancelButton.backgroundColor = kGetColor(243, 243, 243);
        [_cancelButton addTarget:self action:@selector(dissmiss) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cancelButton];
    }
    return self;
}


- (void)setPositionInView:(UIView *)view
{
    if([self.buttons count] == 0)
    {
        return;
    }
    float buttonWidth = ((ShareActionSheetButton *)[self.buttons objectAtIndex:0]).frame.size.width;
    float buttonHeight = ((ShareActionSheetButton *)[self.buttons objectAtIndex:0]).frame.size.height;
    self.frame = CGRectMake(0.0f, view.frame.size.height, view.frame.size.width, kCancelButtonHeight + kBottomHeight + kHeaderHeight + (buttonHeight + kIntervalWithButtonsY)*(([self.buttons count]-1)/kButtonCountPerRow + 1));
    self.backgroundImageView.frame = CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height);
    float beginX = (self.frame.size.width - (kIntervalWithButtonsX * (kButtonCountPerRow - 1) + buttonWidth * kButtonCountPerRow))/2;
    
    _cancelButton.frame = CGRectMake(0,self.bounds.size.height-kCancelButtonHeight,kScreenWidth, kCancelButtonHeight);
    
    if ([self.buttons count] > kButtonCountPerRow)
    {
        for (int i = 0; i < [self.buttons count]; i++)
        {
            ShareActionSheetButton * button = [self.buttons objectAtIndex:i];
            button.frame = CGRectMake(beginX + i%kButtonCountPerRow*(buttonWidth + kIntervalWithButtonsX), kHeaderHeight + i/kButtonCountPerRow*(buttonHeight + kIntervalWithButtonsY), buttonWidth, buttonHeight);
        }
    }
    else
    {
        // 计算空间的位置
        float intervalX = (self.frame.size.width - buttonWidth*[self.buttons count])/([self.buttons count] + 1);
        for (int i = 0; i < [self.buttons count]; i++)
        {
            ShareActionSheetButton * button = [self.buttons objectAtIndex:i];
            button.frame = CGRectMake(intervalX + i%kButtonCountPerRow*(buttonWidth + intervalX), kHeaderHeight + i/kButtonCountPerRow*(buttonHeight + kIntervalWithButtonsY), buttonWidth, buttonHeight);
        }
        
    }
}


- (void)showInView:(UIView *)view
{
    [self setPositionInView:view];
    [view addSubview:_coverView];
    [view addSubview:self];
    [UIView beginAnimations:@"ShowShareActionSheet" context:nil];
    self.frame = CGRectMake(0.0f, self.frame.origin.y - self.frame.size.height, self.frame.size.width, self.frame.size.height);
    _coverView.hidden = NO;
    [UIView commitAnimations];
}

- (void)dissmiss
{
    [UIView beginAnimations:@"DismissShareActionSheet" context:nil];
    self.frame = CGRectMake(0.0f, self.frame.origin.y + self.frame.size.height, self.frame.size.width, self.frame.size.height);
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(sheetDidDismissed)];
    _coverView.hidden = YES;
    [UIView commitAnimations];
}

- (void)sheetDidDismissed
{
    [_coverView removeFromSuperview];
    [self removeFromSuperview];
}

- (void)buttonAction:(UIButton *)button
{
    NSLog(@"index:%ld",(long)button.tag);
    if([delegate respondsToSelector:@selector(choseAtIndex:)])
    {
        [delegate choseAtIndex:button.tag];
    }
    [self dissmiss];
}

@end

@implementation ShareActionSheetButton
@synthesize imgButton;
@synthesize titleLabel;

- (id)init
{
    self = [[[NSBundle mainBundle] loadNibNamed:@"ShareActionSheetButton" owner:self options:nil] objectAtIndex:0];
    for (id obj in self.subviews) {
        if([obj isKindOfClass:[UIButton class]])
        {
            self.imgButton = obj;
        }
        else if([obj isKindOfClass:[UILabel class]])
        {
            self.titleLabel = obj;
        }
    }
    return self;
}

+ (ShareActionSheetButton *)buttonWithImage:(UIImage *)image title:(NSString *)title
{
    ShareActionSheetButton * button = [[ShareActionSheetButton alloc] init];
    [button.imgButton setBackgroundImage:image forState:UIControlStateNormal];
    button.titleLabel.text = title;
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button.titleLabel setTextColor:[UIColor blackColor]];
    return button;
}

@end